package ATM;

import javax.swing.*;
import javax.swing.plaf.metal.MetalIconFactory;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransferJpanel extends JPanel implements ActionListener {
    private Icon pane = Util.getimagesIcon("/images/pane.jpg");
    private Icon buttonOff = Util.getimagesIcon("/images/buttonOff.jpg");
    private JLabel jLabelTrans;
    private JLabel transfer;

    private JButton jButtonTrans;
    private JTextField jTextFieldTrans;

    private JLabel jLabelMoney;
    private JTextField jTextFieldMoney;
    private JButton btnOff;
    private ATMJframe atmJframe;

    public TransferJpanel(ATMJframe atmJframe) {
        this.atmJframe = atmJframe;
        this.setLayout(null);
        this.setOpaque(false);
        jLabelTrans = new JLabel("请输入要转账的ID：");
        jLabelTrans.setBounds(300, 200, 250, 50);
        jLabelTrans.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jLabelTrans);

        jTextFieldTrans = new JTextField();
        jTextFieldTrans.setBounds(520, 200, 300, 50);
        jTextFieldTrans.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jTextFieldTrans);


        //
        jLabelMoney = new JLabel("请输入转账金额：");
        jLabelMoney.setBounds(300, 250, 250, 50);
        jLabelMoney.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jLabelMoney);

        jTextFieldMoney = new JTextField();
        jTextFieldMoney.setBounds(520, 250, 300, 50);
        jTextFieldMoney.setFont(new Font("宋体", Font.BOLD, 25));
        this.add(jTextFieldMoney);


        transfer = new JLabel("转  账");
        transfer.setBounds(330, 300, 140, 50);
        transfer.setFont(new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, 25));
        transfer.setForeground(Color.WHITE);
        this.add(transfer);

        jButtonTrans = new JButton("转账", pane);
        jButtonTrans.setBounds(300, 300, 140, 50);
        jButtonTrans.setOpaque(false);
        this.add(jButtonTrans);
        jButtonTrans.addActionListener(this);


        btnOff = new JButton("", buttonOff);
        btnOff.setBounds(850, 550, 140, 50);
        this.add(btnOff);
        btnOff.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnOff) {
            atmJframe.switchPanel("menu");
        } else if (e.getSource() == jButtonTrans) {
            double money = Double.parseDouble(jTextFieldMoney.getText());

            for (int i = 0; i < atmJframe.getUsersArrayList().size(); i++) {
                if (jTextFieldTrans.getText().equals(atmJframe.getUsersArrayList().get(i).getId())) {
                    if (money > atmJframe.getUser().getCount()) {
                        JOptionPane.showMessageDialog(this, "余额不足，转账失败");
                    } else {
                        atmJframe.getUser().setCount(atmJframe.getUser().getCount()-money);
                        atmJframe.getUsersArrayList().get(i).setCount(atmJframe.getUsersArrayList().get(i).getCount()+money);
                    }
                    return;
                }

            }
            JOptionPane.showMessageDialog(this, "查无此人");


        }
    }
}
